﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Dynamic;
using M;
using Mysoft.MyCM.Scm;

namespace Mysoft.MyCM.Business.Scm
{
	public class VisitBLL : BaseBLL
	{
		/// <summary>
		/// 将访问记录添加到历史表中
		/// </summary>
		/// <param name="visit"></param>
		/// <returns></returns>
		public Guid UpdateVisit(Visit visit)
		{
			MValidator.CheckForNullReference("访问记录", visit);
			using (var client = ScmWebApiClientManager.Manager.GetClient())
			{
				//var result = client.PostSync<ApiResult<Guid>>("p/Visit/UpdateVisit/", visit);
				var result = client.PostAsDCJsonSync<ApiResult<Guid>, Visit>("p/Visit/UpdateVisit/", visit);
				CheckResult(result);
				return result.Result;
			}
		}
		/// <summary>
		/// 获取用户的访问记录
		/// </summary>
		/// <param name="userCode">用户代码</param>
		/// <param name="top">请求的最新个数</param>
		/// <returns></returns>
		public List<Visit> GetVisitHistory(string userCode, int top)
		{
			MValidator.CheckForEmptyString("用户信息", userCode);
			if (top <= 0) top = 1;
			using (var client = ScmWebApiClientManager.Manager.GetClient())
			{
				var result = client.GetSync<ApiResult<List<Visit>>>(string.Format("g/Custom/GetVisitCustom/{0}/{1}", userCode, top));
				CheckResult(result);
				return result.Result;
			}
		}
		/// <summary>
		/// 删除最近访问记录
		/// </summary>
		/// <param name="visitId"></param>
		public void RemoveVisitHistory(Guid visitId)
		{
			MValidator.CheckForEmptyGUID("访问记录", visitId);
			using (var client = ScmWebApiClientManager.Manager.GetClient())
			{
				var result = client.GetSync<ApiResult<string>>(string.Format("g/Visit/DeleteVisit/{0}", visitId));
				CheckResult(result);
			}
		}
	}
}
